﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
using System.Collections.Specialized;
using System.Drawing;

namespace Blob.Uploader
{
	public class BlobClient
	{
		CloudStorageAccount account;
		CloudBlobClient client;
		StringBuilder output = new StringBuilder();

		public BlobClient(string accountName, string accountKey)
		{
			account = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), false);

			client = account.CreateCloudBlobClient();
		}

		public StringBuilder Output { get { return output; } }

		private void Log(string message)
		{
			if (Output != null)
			{
				Output.AppendLine(message);
			}
		}

		public bool TestConnection()
		{
			try
			{
				var containers = client.ListContainers();
				
				Log("Existing Containers:");
				foreach (var c in containers)
				{
					Log("\t" + c.Name);
				}

				return true;
			}
			catch (Exception ex)
			{
				Log("Error:");
				Log(ex.Message);

				return false;
			}
		}

		public CloudBlobContainer EnsureContainerExists(string containerName)
		{
			var container = client.GetContainerReference(containerName.ToLower());
			container.CreateIfNotExist();

			var permissions = container.GetPermissions();
			permissions.PublicAccess = BlobContainerPublicAccessType.Container;
			container.SetPermissions(permissions);

			return container;
		}

		public List<string> GetExistingBlobNames(string containerName)
		{
			List<string> existingNames = new List<string>();

			var container = EnsureContainerExists(containerName);

			var blobs = container.ListBlobs(new BlobRequestOptions()
			{
				  UseFlatBlobListing = true,
				  BlobListingDetails = BlobListingDetails.All
			});

			foreach (var blobItem in blobs)
			{
				existingNames.Add(Path.GetFileName(blobItem.Uri.AbsolutePath));

				Log(Path.GetFileName(blobItem.Uri.AbsolutePath));
			}

			return existingNames;
		}

		public void SaveImage(string containerName, string fileName, string filePath, string thumbnailName, string contentType)
		{
			var container = client.GetContainerReference(containerName.ToLower());

			// Create a blob in container and upload image bytes to it
			var blob = container.GetBlobReference(fileName.ToLower());

			blob.Properties.ContentType = contentType;
			blob.UploadFile(filePath);
			
			Log("Uploaded file: " + fileName);
		}

		public void SaveImage(string containerName, string fileName, Stream stream, string thumbnailName, string contentType)
		{
			var container = client.GetContainerReference(containerName.ToLower());

			// Create a blob in container and upload image bytes to it
			var blob = container.GetBlobReference(fileName.ToLower());

			blob.Properties.ContentType = contentType;
			blob.UploadFromStream(stream);

			Log("Uploaded file: " + fileName);
		}
	}
}
